﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GestionePolveri2.DAL;
using ControlsLib;
using GestionePolveri2.Reports;
using ExceptionReporting;
using GestionePolveriLib.BL;
using System.Threading;
using ControlsLib.SplashScreen;

namespace GestionePolveri2
{
    public partial class GestionePolveri : Form
    {
        Dictionary<string, Form> m_formCash = new Dictionary<string, Form>();
        System.Security.Principal.IPrincipal m_operatore = null;

        public GestionePolveri()
        {
            InitializeComponent();

            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
        }

        void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            ExceptionReporter reporter = new ExceptionReporter();
            reporter.Show(e.Exception);
        }

        protected override void OnLoad(EventArgs e)
        {
            // qui apre la finesta di login...
            base.OnLoad(e);

            DoLogin();
        }

        private void DoLogin()
        {
            LoginForm lf = new LoginForm();
            lf.ApplicationShutdown += new EventHandler(lf_ApplicationShutdown);
            if (lf.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.Text = string.Format("Gestione Polveri [Operatore: {0}]", lf.Operatore.UserName);

                m_operatore = System.Threading.Thread.CurrentPrincipal;
                if (m_operatore.IsInRole("Acquisti"))
                {
                    caricoToolStripMenuItem.Visible = false;
                    scaricoToolStripMenuItem.Visible = false;
                    refresfCacheToolStripMenuItem.Visible = false;
                    anagraficheToolStripMenuItem.Visible = false;
                }
                if (m_operatore.IsInRole("Direzione"))
                {
                    caricoToolStripMenuItem.Visible = false;
                    scaricoToolStripMenuItem.Visible = false;
                    amministrazione2ToolStripMenuItem.Visible = false;
                    refresfCacheToolStripMenuItem.Visible = false;
                    anagraficheToolStripMenuItem.Visible = false;
                }
                if (m_operatore.IsInRole("Produzione"))
                {
                    caricoToolStripMenuItem.Visible = false;
                    scaricoToolStripMenuItem.Visible = false;
                    amministrazione2ToolStripMenuItem.Visible = false;
                    refresfCacheToolStripMenuItem.Visible = false;
                    anagraficheToolStripMenuItem.Visible = false;
                }

                if (m_operatore.IsInRole("Verniciatura"))
                {
                    amministrazione2ToolStripMenuItem.Visible = false;
                    refresfCacheToolStripMenuItem.Visible = false;
                    anagraficheToolStripMenuItem.Visible = false;
                }
            }
        }

        void lf_ApplicationShutdown(object sender, EventArgs e)
        {
            this.Close();
        }

        #region Form Handlers...
        private bool CheckOpenForm(string formName)
        {
            bool retValue = false;

            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == formName)
                {
                    retValue = true;
                    f.Focus();
                    break;
                }
            }

            return retValue;
        }

        private Form ShowForm<T>() where T : Form, new()
        {
            T ar = default(T);

            try
            {
                System.Threading.Thread.CurrentPrincipal = m_operatore;

                if (!System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated)
                {
                    MessageBox.Show("E' necessario effettuare il Login !");
                    return null;
                }

                if (!CheckOpenForm(typeof(T).Name))
                {
                    ar = new T();

                    ar.MdiParent = this;
                    ar.WindowState = FormWindowState.Maximized;

                    ((IForm)ar).InitializeForm();

                    ar.Show();
                }

                return ar;
            }
            catch (System.Security.SecurityException sex)
            {
                MessageBox.Show("Accesso non autorizzato !");
                return null;
            }
            catch (Exception ex)
            {
                if (ex.InnerException.GetType() == typeof(System.Security.SecurityException))
                {
                    MessageBox.Show("Accesso non autorizzato !");
                }
                return null;
            }
        }

        private void ShowForm<T>(bool checkAuthentication) where T : Form, new()
        {
            T ar = default(T);

            try
            {
                if (checkAuthentication)
                {
                    if (!System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated)
                    {
                        MessageBox.Show("E' necessario effettuare il Login !");
                        return;
                    }
                }

                if (!CheckOpenForm(typeof(T).Name))
                {
                    ar = new T();

                    ar.MdiParent = this;
                    ar.WindowState = FormWindowState.Maximized;

                    ((IForm)ar).InitializeForm();

                    ar.Show();
                }
            }
            catch (System.Security.SecurityException sex)
            {
                MessageBox.Show("Accesso non autorizzato !");
            }
            catch (Exception ex)
            {
                if (ex.InnerException.GetType() == typeof(System.Security.SecurityException))
                {
                    MessageBox.Show("Accesso non autorizzato !");
                }
            }
        }
        #endregion

        #region Menu Handlers...
        private void tinteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //ShowAnagrafica<TintaManager>();
        }

        private void polveriToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //ShowAnagrafica<AnagraficaPolveri>();
        }

        private void operatoriToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm<AnagraficaOperatori>(false);
        }

        private void test2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //ShowAnagrafica<Test2>();
        }

        private void impiantiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm<AnagraficaImpianti>();
        }

        private void scatoleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm<AnagraficaScatole>();
        }

        private void fornitoriToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm<AnagraficaFornitori>();
        }

        private void famiglieColoriToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm<AnagraficaFamigliaColori>();
        }

        private void coloriToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm<AnagraficaColori>();
        }

        private void polveriToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ShowForm<AnagraficaPolveri2>();
        }

        private void caricoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm<CaricoMovimenti>();
        }

        private void scaricoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm<ScaricoMovimenti>();
        }

        private void composizioneColoriExtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm<ComposizioneColoreManagerExt2>();
        }

        private void amministrazioneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm<CronologiaMovimenti>();
        }

        private void amministrazione2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm<Amministrazione2>();
        }

        private void tracciablitàToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm<ReportTracciabilita>();
        }

        private void reportDirezionaleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void affiancaOrizzontaleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void affiancaVerticaleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileVertical);
        }

        private void cascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.Cascade);
        }

        private void disponiIconeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void refresfCacheToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrameworkLib.Utilities.Singleton<GestionePolveriLib.Cache.LookUpData>.Instance.DataLoaded += new EventHandler(Instance_DataLoaded);
            FrameworkLib.Utilities.Singleton<GestionePolveriLib.Cache.LookUpData>.Instance.Refresh();
        }

        private void ruoliToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ShowForm<AnagraficaRuoli>();
        }

        //private void reportDirezionaleToolStripMenuItem1_Click(object sender, EventArgs e)
        //{
        //    ShowForm<Reports.ReportDirezionale>();
        //}

        //private void dettaglioPolveriXXXXToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    ShowForm<ReportDirezionaleDettaglioPolveriXXXX>();
        //}

        private void ruoliToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm<AnagraficaRuoli>();
        }

        private void configurazioneMascheraToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm<FrameworkLib.Configurations.FormsConfiguration.ColumnsConfigurationManager>();
        }
        #endregion

        void Instance_DataLoaded(object sender, EventArgs e)
        {
            FrameworkLib.Utilities.Singleton<GestionePolveriLib.Cache.LookUpData>.Instance.DataLoaded -= new EventHandler(Instance_DataLoaded);
            
            MessageBox.Show("Cache ricaricata !", "Gestione Polveri", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox ab = new AboutBox();
            ab.ShowDialog();
        }

        private void composizioneColoriToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            ShowForm<VerniciatoreScortaMinima>();
        }

        private void reportDirezionaleToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            ShowForm<ReportDirezionaleForm>();
        }

        private void abiltaTastierinoNumericoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.abiltaTastierinoNumericoToolStripMenuItem.Checked = !this.abiltaTastierinoNumericoToolStripMenuItem.Checked;

            //FrameworkLib.Utilities.Singleton<FrameworkLib.GlobalConfiguration>.Instance.Update("AbilitaTastierino", this.abiltaTastierinoNumericoToolStripMenuItem.Checked);
        }


    }
}
